package ru.dadle8.service.impl.util;

import org.springframework.stereotype.Service;
import ru.dadle8.service.api.util.Utils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
public class UtilsImpl implements Utils {

    @Override
    public <T> List<List<T>> getBatches(List<T> collection, int batchSize) {
        return IntStream.iterate(0, i -> i < collection.size(), i -> i + batchSize)
                .mapToObj(i -> collection.subList(i, Math.min(i + batchSize, collection.size())))
                .collect(Collectors.toList());
    }
}
